package com.uwo.house.service.serviceImpl;

import com.uwo.house.beans.request.BindSupervisionReq;
import com.uwo.house.beans.request.DistributeDesignReq;
import com.uwo.house.beans.response.DesignOrderResp;
import com.uwo.house.beans.response.RelatedStuffInfo;
import com.uwo.house.beans.response.RelatedStuffResp;
import com.uwo.house.dao.DesignOrderDao;
import com.uwo.house.dao.SupervisionOrderDao;
import com.uwo.house.service.IDesignOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by cyn1c on 2017/7/24.
 */
@Service
public class DesignOrderService implements IDesignOrderService {
    @Autowired
    DesignOrderDao designOrderDao;

    @Autowired
    SupervisionOrderDao supervisionOrderDao;

    public List<DesignOrderResp> getDesignOrderList(String orderId, String decorationStyle, String distributeFlag, String scheduleStatus) {
        return this.designOrderDao.getDesignOrderList(orderId, decorationStyle, distributeFlag, scheduleStatus);
    }

    @Transactional
    public int distributeDesignOrder(DistributeDesignReq distributeDesignReq) {
        BindSupervisionReq bindSupervisionReq = new BindSupervisionReq();
        bindSupervisionReq.setOrderId(distributeDesignReq.getOrderId());
        bindSupervisionReq.setDesigner(distributeDesignReq.getUserId());
        bindSupervisionReq.setSupervisorId(distributeDesignReq.getSupervisorId());
        supervisionOrderDao.insertSupervisionOrder(bindSupervisionReq);
        return this.designOrderDao.distributeDesignOrder(distributeDesignReq);
    }

    @Transactional
    public int deleteDesign(String orderId) {
        int result2 = 2;
        try {
            designOrderDao.deleteSchedule(orderId);
            designOrderDao.deleteOrderStatus(orderId);
        } catch (Exception e) {
            result2 = 1;
        }
        int result1 = this.designOrderDao.deleteDesign(orderId);
        if (result2 != 1 && result1 == 1) {
            return 1;
        }
        return 2;
    }

    public RelatedStuffResp getRelatedStuff(String orderId){
        return designOrderDao.getRelatedStuff(orderId);
    }

    public RelatedStuffInfo getStuffInfoById(String userId){
        return designOrderDao.getStuffInfoById(userId);
    }

    public String getMobileByUserId(String userId){
        return designOrderDao.getMobileByUserId(userId);
    }
}
